﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageProcessing
{
	public partial class ColorEnhanceDialog : Form
	{
		private Bitmap bmIn;
		private Bitmap bmBaseTh;
		private Bitmap bmModTh;
		private Bitmap bmOut;
		private bool imageReady;
	

		public Bitmap ImageOut
		{
			get { return bmOut; }
			set { bmOut = new Bitmap(value); }
		}
		public Bitmap ImageIn
		{
			set
			{
				bmIn = new Bitmap(value);
				imageReady = true;
				bmBaseTh = (Bitmap)bmIn.GetThumbnailImage(200, 200, null, IntPtr.Zero);
				bmModTh = new Bitmap(bmBaseTh);
				picBoxBase.Invalidate();
				picBoxModified.Invalidate();
			}
		}


		public ColorEnhanceDialog()
		{
			InitializeComponent();
		}

		public ColorEnhanceDialog(Bitmap bmin)
		{
			InitializeComponent();
			ImageIn = bmin;
			ImageOut = bmIn;
		}

		private void picBoxModified_Paint(object sender, PaintEventArgs e)
		{
			if (imageReady)
				picBoxModified.BackgroundImage = bmModTh;
		}

		private void picBoxBase_Paint(object sender, PaintEventArgs e)
		{
			if (imageReady)
				picBoxBase.BackgroundImage = bmBaseTh;
		}

		private void btnCommit_Click(object sender, EventArgs e)
		{
			bmBaseTh = new Bitmap(bmModTh);
			ImageIn = ImageOut;
			picBoxBase.Invalidate();
		}

		private void btnSaturate_Click(object sender, EventArgs e)
		{
			double s;
			
			try
			{
				
				s = Convert.ToDouble(txtboxSaturation.Text);
				if (s > 100.0)
				{
					s = 100.0;
					txtboxSaturation.Text = "100";
				} //end if

				if (s < 0)
				{
					s = 0.0;
					txtboxSaturation.Text = "0";
				} //end if

			} //end try
			catch (Exception)
			{
				txtboxSaturation.Text = "";
				txtboxSaturation.Focus();
				lblSaturationError.Visible = true;
				return;
			} //end catch

			lblSaturationError.Visible = false;
			bmModTh = ColorFilters.Saturation(bmBaseTh, s,true);
			picBoxModified.Invalidate();

			ImageOut = ColorFilters.Saturation(bmIn, s,true);
			
		}

		private void btnHue_Click(object sender, EventArgs e)
		{
			double angle;
			double theta;
			
			try
			{
				lblHueError1.Visible = false;
				angle = Convert.ToDouble(txtboxHueAngle.Text);
				if (angle > 360.0)
				{
					angle = 360.0;
					txtboxHueAngle.Text = "360";
					lblHueError1.Visible = true;
				} //end if

				if (angle < 0)
				{
					angle = 0.0;
					txtboxHueAngle.Text = "0";
					lblHueError1.Visible = true;
				} //end if

				theta = Math.PI * angle / 180;

			} //end try
			catch (Exception)
			{
				txtboxHueAngle.Text = "";
				txtboxHueAngle.Focus();
				lblHueError1.Visible = true;
				return;
			} //end catch

			bmModTh = ColorFilters.Hue(bmBaseTh, theta, true);
			picBoxModified.Invalidate();

			ImageOut = ColorFilters.Hue(bmIn, theta, true);			
		}
	}
}
